﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.dataAcess;
using dhSystemBLL.basicData;
using dhDataAcess.Model;
using baseSet.importExport;
using dhsystem.Main;
using baseSet.power;

namespace dhsystem.basicForm
{
    public partial class FrmCompanyinfo : Form
    {
        companyinfoBLL companyinfo = new companyinfoBLL();
        companyinfoModel companyRow = new companyinfoModel();
        public FrmCompanyinfo()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = true;
            if (lblState.Text == "编辑")
            {
                MessageBox.Show("当前在编辑数据状态，请先保存编辑数据或者取消再进行编辑");
                return;
            }
            lblState.Text = "新增";
            if (groupAdd.Visible == false)
            {
                groupAdd.Visible = true;
                groupList.Width = groupList.Width - 288;
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
            textCname.Focus();

        }
        //打开窗口时显示列表数据并且隐藏新增数据组件
        private void FrmCompanyinfo_Load(object sender, EventArgs e)
        {
            bindingSourceCompanyinfo.DataSource = companyinfo.getcompanyinfoAll();
            dGridCompanyinfo.DataSource = bindingSourceCompanyinfo;
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            DcolumnSet.colSet(moduleInfo .companyinfo ,dGridCompanyinfo );
            toolStripStatusLabel1.Text = "数据总条数:" + dGridCompanyinfo.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.companyinfo );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }
        //取消按钮，隐藏新增数据组件
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            groupList.Width = 288 + groupList.Width;
            groupAdd.Visible = false;
            lblState.Text = "正常";
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (lblState.Text == "新增")
            {
               
                    try
                    {
                        DialogResult result = MessageBox.Show("是否新增新的权限组！", "提示信息", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            companyRow.recordid = Convert.ToInt32(textid.Text);
                            companyRow.cname = textCname.Text;
                            companyRow.cnameByEN = textCnameByEN.Text;
                            companyRow.telPhone = textTelPhone.Text;
                            companyRow.fax = textFax.Text;
                            companyRow.address = textAddress.Text;
                            companyRow.linkman = textLinkman.Text;
                            companyRow.email = textEmail.Text;
                            companyRow.mobile = textMobile.Text;
                            companyRow.mark = textmark.Text;
                            companyinfo.companyinfoInsert(companyRow);
                            bindingSourceCompanyinfo.DataSource = companyinfo.getcompanyinfoAll();
                           
                            if (checkAdd.Checked == true)
                            {
                                lblState.Text = "新增";
                            }
                            else
                            {
                                lblState.Text = "正常";
                            }
                            return;
                        }
                    }
                    catch (FormatException ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
           
            if (lblState.Text == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否更改权限组资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        companyRow.recordid =Convert.ToInt32 ( textid.Text);
                        companyRow.cname = textCname.Text;
                        companyRow.cnameByEN = textCnameByEN.Text;
                        companyRow.telPhone = textTelPhone.Text;
                        companyRow.fax = textFax.Text;
                        companyRow.address = textAddress.Text;
                        companyRow.linkman = textLinkman.Text;
                        companyRow.email = textEmail.Text;
                        companyRow.mobile = textMobile.Text;
                        companyRow.mark = textmark.Text;
                        companyinfo.companyinfoUpdate (companyRow);
                        bindingSourceCompanyinfo.DataSource = companyinfo.getcompanyinfoAll();
                        lblState.Text = "正常";
                        return;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            checkAdd.Visible = false;
            try
            {
                if (lblState.Text == "新增")
                {
                    MessageBox.Show("当前在新增数据状态，请先保存新增数据或者取消再进行编辑");
                    return;
                }

                lblState.Text = "编辑";

                if (groupAdd.Visible == false)
                {
                    groupAdd.Visible = true;
                    groupList.Width = groupList.Width - 288;
                    groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
                }

                DataGridViewRow currentRow = dGridCompanyinfo.CurrentRow;
                textCname.Text = currentRow.Cells["cname"].Value.ToString();
                textCnameByEN.Text = currentRow.Cells["cnameByEN"].Value.ToString();
                textTelPhone.Text = currentRow.Cells["telPhone"].Value.ToString();
                textFax.Text = currentRow.Cells["fax"].Value.ToString();
                textAddress.Text = currentRow.Cells["address"].Value.ToString();
                textLinkman.Text = currentRow.Cells["linkman"].Value.ToString();
                textEmail.Text = currentRow.Cells["email"].Value.ToString();
                textMobile.Text = currentRow.Cells["mobile"].Value.ToString();
                textmark.Text = currentRow.Cells["mark"].Value.ToString();
                textid.Text = currentRow.Cells["recordid"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string recordid = dGridCompanyinfo.Rows[dGridCompanyinfo.CurrentRow.Index].Cells[0].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除权限组编号为" + recordid + "的权限组信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceCompanyinfo.DataSource = companyinfo.companyinfoDel(recordid);
                    dGridCompanyinfo.DataSource = bindingSourceCompanyinfo;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            bindingSourceCompanyinfo.DataSource = companyinfo.getcompanyinfoAll();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.companyinfo );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridCompanyinfo.DataSource = query.rTable;
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSourceCompanyinfo .DataSource =companyinfo.getcompanyinfoAll("%" + textCnameQuery .Text + "%");
                dGridCompanyinfo.DataSource = bindingSourceCompanyinfo;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //工具栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmCompanyinfo_Resize(object sender, EventArgs e)
        {
            if (groupAdd.Visible == true)
            {
                groupAdd.Location = new Point(groupList.Width + 20, groupList.Top);
            }
        }
        //datagridview第一列显示行号
        private void dGridCompanyinfo_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.companyinfo , dGridCompanyinfo , "公司信息");
            frm.ShowDialog();
        }
    }
}
